#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version        : 1.0
# @Create Time    : 2024/7/25 14:39
# @File           : task
# @IDE            : PyCharm
# @desc           : 文件描述

from typing import List, Optional, Union

from pydantic import BaseModel, ConfigDict, Field, computed_field, field_validator

from apps.dispatch.assignment.schemas import TaskAssignUsersSimpleOut, TaskAssignUsers
from apps.dispatch.feedback.schemas import TaskFeedbackSimpleOut, TaskFeedback
from apps.dispatch.team.schemas import TaskWorkTeamSimpleOut, TaskWorkTeam
from apps.dispatch.need.schemas import TaskNeedSimpleOut, TaskNeed
from core.data_types import DatetimeStr, DateStr


class TaskWorker(BaseModel):
    team: str
    userid: Union[str, int]
    username: str
    skills: Optional[str] = None
    status: Optional[str] = None
    available_time: Optional[DatetimeStr] = None


class Task(BaseModel):
    ticket_id: str  # 工单编号

    ao_id: str  # AO编号
    ao_name: str  # AO名称
    ao_type: Optional[str] = None  # AO类型，AAO、DISR、FRR、AO等

    status: str  # AO状态，未开始、生产中、已完工

    product_type: str  # 机型
    flight: str  # 架次
    station: str  # 工位
    sub_station: Optional[str] = None  # 二级工位
    position: Optional[str] = None  # 站位
    team: Optional[str] = None  # 班组
    team_leader: Optional[str] = None

    group1: Optional[str] = None  # 分组1
    group2: Optional[str] = None  # 分组2
    tag: Optional[str] = None  # 标签

    plan_start: Optional[DateStr] = None  # 计划开工时间
    plan_end: Optional[DateStr] = None # 计划完工时间
    plan_user: Optional[str] = None # 计划人员
    plan_date: Optional[DatetimeStr] = None # 计划时间

    start: Optional[DatetimeStr] = None # 实际开工时间
    end: Optional[DatetimeStr] = None  # 实际完工实际
    
    priority: Optional[int] = 0  # AO生产优先级
    order: Optional[int] = 0  # AO展示排序

    material_match: Optional[bool] = None
    delivery_message: Optional[bool] = None
    pi: Optional[bool] = None

    in_daily_plan: Optional[bool] = None
    in_weekly_plan: Optional[bool] = None

    create_user: Optional[str] = None
    update_user: Optional[str] = None


class TaskIn(Task):
    assign_users: List[TaskAssignUsers] = []


class TaskUpdate(BaseModel):
    group1: Optional[str] = None
    group2: Optional[str] = None
    tag: Optional[str] = None
    order: Optional[int] = None

    update_user: Optional[str] = None


class TaskSimpleOut(Task):
    model_config = ConfigDict(from_attributes=True)

    id: int

    update_datetime: DatetimeStr
    create_datetime: DatetimeStr


class TaskOut(TaskSimpleOut):
    model_config = ConfigDict(from_attributes=True)

    depends: Optional[dict] = {}  # 前期依赖任务

    assign_users: List[TaskAssignUsersSimpleOut] = []
    feedback: List[TaskFeedbackSimpleOut] = []

    needs: Optional[List[TaskNeedSimpleOut]] = []
    work_teams: Optional[List[TaskWorkTeamSimpleOut]] = [] # 外协班组

    @field_validator("assign_users", "feedback", "needs", "work_teams", mode="before")
    def filter_delete(cls, v):
        return [i for i in v if not i.is_delete]


class TaskAutoUpdate(BaseModel):
    model_config = ConfigDict(from_attributes=True)
    
    priority: Optional[int] = 99
    status: Optional[str] = None
    material_match: Optional[bool] = None
    delivery_message: Optional[bool] = None
    pi: Optional[bool] = None
    in_daily_plan: Optional[bool] = None
    in_weekly_plan: Optional[bool] = None
    start: Optional[DatetimeStr] = None
    end: Optional[DatetimeStr] = None
    team: Optional[str] = None
    team_leader: Optional[str] = None
    sub_station: Optional[str] = None


class TaskQueryOut(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    ao_id: str
    ao_name: str
    team: Optional[str] = None
    
    value: str = Field(alias="ticket_id")

    @computed_field
    def label(self) -> str:
        return f"{self.value} {self.ao_name}({self.ao_id})"



class TaskAssignUsersOut(TaskSimpleOut):
    model_config = ConfigDict(from_attributes=True)

    assign_users: List[TaskAssignUsersSimpleOut] = []


class TaskOutWithFeebacks(TaskSimpleOut):
    model_config = ConfigDict(from_attributes=True)

    feedback: List[TaskFeedbackSimpleOut] = []

    @field_validator("feedback", mode="before")
    def filter_delete(cls, v):
        return [i for i in v if not i.is_delete]

class TaskOutWithAssignments(TaskSimpleOut):
    model_config = ConfigDict(from_attributes=True)

    assign_users: List[TaskAssignUsersSimpleOut] = []

    @field_validator("assign_users", mode="before")
    def filter_delete(cls, v):
        return [i for i in v if not i.is_delete]
